package frsf.cidisi.exercise.modelo.search.actions;

import frsf.cidisi.exercise.modelo.search.AgenteEstado;
import frsf.cidisi.exercise.modelo.search.AmbienteEstado;
import frsf.cidisi.exercise.modelo.util.Coordenada;
import frsf.cidisi.faia.agent.search.SearchAction;
import frsf.cidisi.faia.agent.search.SearchBasedAgentState;
import frsf.cidisi.faia.state.AgentState;
import frsf.cidisi.faia.state.EnvironmentState;

public class LevantarLlave extends SearchAction {

    /**
     * This method updates a tree node state when the search process is running.
     * It does not updates the real world state.
     */
    @Override
    public SearchBasedAgentState execute(SearchBasedAgentState s) {
        AgenteEstado agState = (AgenteEstado) s;
        
        if(!agState.getTieneLlave()){
                Coordenada posicion = agState.getPosAgente();
                if(agState.getLaberinto().getCelda(posicion).hasLlave()){
                        agState.setTieneLlave(true);
                        return agState;
                }
        }
        
        return agState;
    }

    /**
     * This method updates the agent state and the real world state.
     */
    @Override
    public EnvironmentState execute(AgentState ast, EnvironmentState est) {
        
        AmbienteEstado environmentState = (AmbienteEstado) est;
        AgenteEstado agState = ((AgenteEstado) ast);

                if(!agState.getTieneLlave()){
                Coordenada posicion = agState.getPosAgente();
                if(agState.getLaberinto().getCelda(posicion).hasLlave()){
                        agState.setTieneLlave(true);
                        environmentState.removeLlave(posicion);
                }
        }

        return environmentState;
    }

    /**
     * This method returns the action cost.
     */
    @Override
    public Double getCost() {
        return new Double(0);
    }

    /**
     * This method is not important for a search based agent, but is essensial
     * when creating a calculus based one.
     */
    @Override
    public String toString() {
        return "LevantarLlave";
    }
}